package com.atguigu.schedule.service.impl;

import com.atguigu.schedule.mapper.FaceDetectionRecordMapper;
import com.atguigu.schedule.pojo.FaceDetectionRecord;
import com.atguigu.schedule.service.FaceDetectionRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 人脸检测记录服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FaceDetectionRecordServiceImpl implements FaceDetectionRecordService {
    
    private final FaceDetectionRecordMapper faceDetectionRecordMapper;
    
    @Override
    @Transactional
    public FaceDetectionRecord saveDetectionRecord(FaceDetectionRecord record) {
        try {
            // 设置创建时间
            if (record.getCreatedAt() == null) {
                record.setCreatedAt(LocalDateTime.now());
            }
            
            int result = faceDetectionRecordMapper.insert(record);
            if (result > 0) {
                log.info("保存人脸检测记录成功: 用户={}, 置信度={}, 时间={}", 
                        record.getUsername(), record.getConfidence(), record.getDetectionTime());
                return record;
            } else {
                throw new RuntimeException("保存人脸检测记录失败");
            }
        } catch (Exception e) {
            log.error("保存人脸检测记录失败", e);
            throw new RuntimeException("保存人脸检测记录失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<FaceDetectionRecord> findByUsername(String username, int offset, int limit) {
        return faceDetectionRecordMapper.selectByUsernameWithPaging(username, offset, limit);
    }
    
    @Override
    public List<FaceDetectionRecord> findByUsernameAndTimeRange(String username, LocalDateTime startTime, LocalDateTime endTime, int offset, int limit) {
        return faceDetectionRecordMapper.selectByUsernameAndTimeRangeWithPaging(username, startTime, endTime, offset, limit);
    }
    
    @Override
    public List<FaceDetectionRecord> findByTimeRange(LocalDateTime startTime, LocalDateTime endTime, int offset, int limit) {
        return faceDetectionRecordMapper.selectByTimeRangeWithPaging(startTime, endTime, offset, limit);
    }
    
    @Override
    public List<FaceDetectionRecord> findAll(int offset, int limit) {
        // 由于MyBatis没有直接的分页查询所有记录的方法，这里使用时间范围查询
        return faceDetectionRecordMapper.selectAll();
    }
    
    @Override
    public long countByUsername(String username) {
        return faceDetectionRecordMapper.countByUsername(username);
    }
    
    @Override
    public FaceDetectionRecord getLatestDetectionByUsername(String username) {
        return faceDetectionRecordMapper.selectLatestByUsername(username);
    }
    
    @Override
    public List<String> getAllUsernames() {
        return faceDetectionRecordMapper.selectDistinctUsernames();
    }
    
    @Override
    @Transactional
    public void deleteRecordsBefore(LocalDateTime beforeTime) {
        try {
            // 使用MyBatis直接删除过期记录
            // 这里需要在Mapper中添加按时间删除的方法
            log.info("删除过期的人脸检测记录，截止时间: {}", beforeTime);
            // 注意：这里需要实现按时间删除的方法
        } catch (Exception e) {
            log.error("删除过期记录失败", e);
            throw new RuntimeException("删除过期记录失败: " + e.getMessage());
        }
    }
}

